package com.power.sifa.approve.dao.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.power.sifa.approve.model.Apl_applicant;
import com.power.sifa.approve.model.pageInfo.Apl_applicantPageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.power.sifa.approve.dao.Apl_applicantDaoCustom;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 申请人 持久层实现(hibernate)
*/
@Slf4j
@Repository
public class Apl_applicantDaoImpl implements Apl_applicantDaoCustom{

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> getAppperAndThiperByApplyid(String applyid) {
        String sql = "select id, name from apl_applicant where applyid = ? union all select id, name from apl_thirdper where applyid = ?";

        return jdbcTemplate.queryForList(sql, new Object[]{applyid, applyid});
    }

    @Override
    public List<Apl_applicant> getCardnoPendingList(Apl_applicantPageInfo pageInfo){
       List<Apl_applicant> result = new ArrayList<>();
       String sql ="select m.subtime,m.acceptcode,m.status,a.applyname,a.targetname from apl_main m left join apl_admreview a on m.applyname = a.applyname where m.status not in ('6000608','6000609') and  m.applyname in (select name from apl_applicant where cardno = $1 )";
       sql = sql.replace("$1","'"+pageInfo.getF_cardno()+"'");
       log.info(sql);
       List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
       if(CollectionUtil.isNotEmpty(list)){
           result = JSON.parseArray(JSON.toJSONString(list), Apl_applicant.class);
       }
       return result;
    }

    @Override
    public List getCardnoProcessedList(Apl_applicantPageInfo pageInfo) {
        List<Apl_applicant> result = new ArrayList<>();
        String sql ="select m.subtime,m.acceptcode,m.status,a.applyname,a.targetname from apl_main m left join apl_admreview a on m.applyname = a.applyname where m.status in ('6000608','6000609') and  m.applyname in (select name from apl_applicant where cardno = $1 )";
        sql = sql.replace("$1","'"+pageInfo.getF_cardno()+"'");
        log.info(sql);
        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
        if(CollectionUtil.isNotEmpty(list)){
            result = JSON.parseArray(JSON.toJSONString(list), Apl_applicant.class);
        }
        return result;
    }
}
